﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClusHal
{
    class Approximation
    {
        private int decisionClass;

        public int DecisionClass
        {
            get { return decisionClass; }
            set { decisionClass = value; }
        }

        private ERelation direction;

        public ERelation Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        private List<Action> lower;

        internal List<Action> Lower
        {
            get { return lower; }
        }

        private List<Action> upper;

        internal List<Action> Upper
        {
            get { return upper; }
        }

        public Approximation(List<Action> data,int cl, ERelation dir)
        {
            decisionClass = cl;
            direction = dir;
            lower=new List<Action>();
            upper=new List<Action>();
            if (dir == ERelation.AtLeast)
            {
                foreach (Action a in data)
                {
                    if (a.CountDominatedByClassAM(cl) <= 0)
                        lower.Add(a);
                    if (a.CountDominatesByClassAL(cl) > 0)
                        upper.Add(a);
                }
            }
            if (direction == ERelation.AtMost)
            {
                foreach (Action a in data)
                {
                    if (a.CountDominatesByClassAM(cl) <= 0)
                        lower.Add(a);
                    if (a.CountDominatedByClassAL(cl) > 0)
                        upper.Add(a);
                }
            }
        }

        public int BoundarySize
        {
            get
            {
                return upper.Count - lower.Count;
            }
        }

        public Set<Action> Boundary
        {
            get
            {
                Set<Action> s = new Set<Action>();
                foreach (Action a in upper)
                    if (lower.IndexOf(a) < 0)
                        s.Add(a);
                return s;
            }
        }
          

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Lower(");
            sb.Append(decisionClass.ToString());
            sb.Append(")");
            if (direction == ERelation.AtLeast)
                sb.Append(">=");
            else
                sb.Append("<=");
            sb.Append(": {");
            foreach (Action a in lower)
            {
                sb.Append(a.ToString());
                sb.Append(",");
            }
            sb.Append("}");
            sb.AppendLine();
            sb.Append("Upper(");
            sb.Append(decisionClass.ToString());
            sb.Append(")");
            if (direction == ERelation.AtLeast)
                sb.Append(">=");
            else
                sb.Append("<=");
            sb.Append(": {");
            foreach (Action a in upper)
            {
                sb.Append(a.ToString());
                sb.Append(",");
            }
            sb.Append("}");

            return sb.ToString();
        }
    }
}
